﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_LayoutIndex.cshtml";
}@using LeSoft.Application.Code;
<script>
    var Mode = "@ViewBag.Mode";
    var keyValue = request('keyValue');   
    var _organizeid = "@LeSoft.Application.Code.OperatorProvider.Provider.Current().CompanyId";
    var _state = -1;

    $(function () {
        initControl();
        GetGrid();
        //获取表单
        if (!!keyValue) {
            T = "E";
            $.SetForm({
                url: "../../ResourceManage/RS_PStructPublish/GetPStructPublishBillEntity",
                param: { keyValue: keyValue },
                success: function (data) {
                    $("#PSBillfrom").SetWebControls(data.data1);
                    $("#gridTable")[0].addJSONData(data.data2);
                    _organizeid = data.data1.OrganizeId;
                    _state = data.data1.State;

                    if (data.data1.State == "1") {

                    }
                    if (data.data1.State == "2") {
                        $(".BaseClass").attr("disabled", "disabled");
                        $(".btn-success").attr("disabled", "disabled");
                        $("#btn_next").attr("disabled", "disabled");
                    }
                    if (data.data1.State == "3" || data.data1.State == "4" || data.data1.State == "5") {
                        $(".AiditInfo").css({ "backgroundColor": "#FFF29D" });
                        var str = "审核人：" + data.data1.CheckMan + "&nbsp;&nbsp;审核日期：" + data.data1.CheckDate + "&nbsp;&nbsp;审核说明：" + data.data1.CheckMemo;
                        $(".AiditInfo").html(str);
                        $(".BaseClass").attr("disabled", "disabled");
                        $(".btn-success").attr("disabled", "disabled");
                        $("#btn_next").attr("disabled", "disabled");                       
                    }
                }
            });
        }
    })
    //初始化控件
    function initControl() {
        if (Mode == "True") {
            $("#Code").attr("placeholder", "系统自编号不可编辑")
            $("#Code").attr("readonly", "readonly")
        } else {
            $("#Code").attr("isvalid", "yes")
            $("#Code").attr("checkexpession", "NotNull")
        }
        //组织机构
        $("#OrganizeId").ComboBoxTree({
            url: "../../BaseManage/Organize/GetTreeJsonById",
            height: "250px",
            description: "===选择组织机构===",
            click: function (item) {
                _organizeid = item.id;
            }
        });
        $("#OrganizeId").ComboBoxSetValue(_organizeid);
        $("#Category").ComboBox({
            data: [{ "key": "1", "value": "发布" }, { "key": "2", "value": "撤回" }],
            id: "key",
            text: "value",
            description: "=选择业务类型=",
            height: "150px",
            click: function (item) {
                if ($("#Category").attr("data-value") == 1) {
                    $(".CS-T").removeAttr("hidden");
                }
                else {
                    $(".CS-T").attr("hidden", "hidden");
                }
            }
        });
        $("#Category").ComboBoxSetValue("1");
      
    }

    function GetGrid() {
        var $gridTable = $("#gridTable");
        $gridTable.jqGrid({
            datatype: "json",
            height: 420,
            autowidth: true,
            unwritten: false,
            colModel: [
                { label: "Id", name: "Id", hidden: true },
                { label: "PStructId", name: "PStructId", hidden: true }, //RoomDetail
                { label: "RoomDetail", name: "RoomDetail", index: "RoomDetail", hidden: true },
                { label: "名称", name: "BillId", index: "BillId", hidden: true },
               // { label: "小区名称", name: " ", index: " ", width: 100, align: "center", sortable: false },
                { label: "房间名称", name: "Pavilionion", index: "Pavilionion", width: 200, align: "center", sortable: false },
               // { label: "房屋名称", name: "RoomName", index: "RoomName", width: 100, align: "center", sortable: false },
                { label: "房屋编号", name: "RoomCode", index: "RoomCode", width: 100, align: "center", sortable: false },
                { label: "房屋状态", name: "RoomState", index: "RoomState", hidden: true },
                { label: "发布状态", name: "IsPublish", index: "IsPublish", hidden: true },
                {
                    label: "房屋状态", name: "RoomStateStr", index: "RoomStateStr", width: 100, align: "center", sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        switch (rowObject.RoomState) {
                            case "0":
                            case 0: return "准备房";
                            case "1":
                            case 1: return "待租";
                            case "2":
                            case 2: return "已选取";
                            case "3":
                            case 3: return "已租";
                            case "4":
                            case 4: return "自用";
                            case "5":
                            case 5: return "已售";
                            case "6":
                            case 6: return "续租中";
                            case "7":
                            case 7: return "维修中";
                            default: return "---";

                        }
                    }
                },
                
                //{
                //    label: "发布状态", name: "IsPublishStr", index: "IsPublishStr", width: 100, align: "center", sortable: false,
                //    formatter: function (cellvalue, options, rowObject) {
                        //switch (rowObject.IsPublish) {
                        //    case "0":
                        //    case 0: return "未发布";
                        //    case 1:
                        //    case "1":
                        //    case "2":
                        //    case 2: return "已发布";
                        //    case "3":
                        //    case 3: return "发布失败";
                        //    case "4":
                        //    case 4: return "已撤回";
                        //    case "5":
                        //    case 5: return "撤回失败";
                        //    default: return rowObject.IsPublish;

                //        }
                //    }
                //},
                {
                    label: "户型", name: "RoomType", index: "RoomType", width: 100, align: "center", sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        switch (cellvalue) {
                            case "1": case 1: return "一室";
                            case "2": case 2: return "一室一厅";
                            case "3": case 3: return "二室";
                            case "4": case 4: return "二室一厅";
                            case "5": case 5: return "三室";
                            case "6": case 6: return "三室一厅";
                            case "7": case 7: return "四室";
                            case "8": case 8: return "五室";
                            default: return cellvalue;

                        }
                    }
                },   
                { label: "建筑面积", name: "Area", index: "Area", width: 100, align: "center", sortable: false },
                {
                    label: "起租日期", name: "EMoveInDate", index: "EMoveInDate", width: 100, align: "center", sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        return formatDate(cellvalue, 'yyyy-MM-dd');
                    }
                },
                { label: "月租金", name: "MonthlyRent", index: "MonthlyRent", width: 100, align: "center", sortable: false },               
                {
                    label: "操作PC端", name: "IsPublishToPC", index: "IsPublishToPC", width: 50, align: "center",
                    formatter: function (cellvalue, options, rowObject) {                     
                        return cellvalue == 1 ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                    },
                },            
                {
                    label: "操作其他平台", name: "IsPublishToOther", index: "IsPublishToOther", width: 50, align: "center",
                    formatter: function (cellvalue, options, rowObject) {
                        return cellvalue == 1 ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                    },
                },
                 {
                     label: "操作状态", name: "IsPublishStr", index: "IsPublishStr", width: 200, align: "center", sortable: false,
                     formatter: function (cellvalue, options, rowObject) {
                         debugger;
                         var str = "发布撤回情况：";
                         if (rowObject.IsPublishToPC == 1) {
                             str += "已操作pc端";
                             switch (rowObject.IsPublish) {
                                 case 1:
                                 case 2: str += "且发布成功"; break;
                                 case 3: str += "且发布失败"; break;
                                 case 4: str += "且成功撤回"; break;
                                 case 5: str += "且撤回失败"; break;
                                 default: str += ""; break;
                             }
                         }
                         else {
                             str += "未操作pc端";
                         }

                         if (rowObject.IsPublishToOther == 1) {
                             str += "；已操作其他平台";
                             switch (rowObject.IsPublishOther) {
                                 case 1:
                                 case 2: str += "且发布成功"; break;
                                 case 3: str += "且发布失败"; break;
                                 case 4: str += "且成功撤回"; break;
                                 case 5: str += "且撤回失败"; break;
                                 default: str += ""; break;
                             }
                         }
                         else {
                             str += "；未操作其他平台：";
                         }

                         return str;
                     }
                 },

            ],
            caption: "房屋明细",
            rowNum: 10000,
            rownumbers: true,
            shrinkToFit: false,
            gridview: true,
            hidegrid: false,
            gridview: true,
            subGrid: true,
            onCellSelect: function (rowid, iCol, cellcontent, e) {
                debugger;
                if (_state<2&&(iCol == 15 || iCol == 16)) {
                    if (cellcontent == "<i class=\"fa fa-toggle-on\"></i>") $gridTable.setCell(rowid, iCol, "0");
                    else $gridTable.setCell(rowid, iCol, "1");
                }
            },
            subGridRowExpanded: function (subgrid_id, row_id) {
                var PStructId = "";
                if (T == "S") {
                    PStructId = $gridTable.getCell(row_id, "Id");
                }
                if (T == "E") {
                    PStructId = ($gridTable.getCell(row_id, "PStructId"));
                }
                var subgrid_table_id = subgrid_id + "_t";
                var subhtml = "";
                subhtml += '<div><table id="' + subgrid_table_id + '"></table></table></div>';
                $("#" + subgrid_id).html(subhtml);
                $("#" + subgrid_table_id).jqGrid({
                    caption: "房屋物品列表",
                    hidegrid: false,
                    datatype: "json",
                    url: "../../ResourceManage/RS_PStructPublish/GetRoomDetial?KeyValue=" + PStructId + "&IsSubmit=" + _state + "&BillId=" + keyValue,
                    height: 170,
                    colModel: [
                     { label: "Id", name: "Id", hidden: true },
                     { label: "物品名称", name: "Name", width: 160, align: "left", sortable: false },
                     { label: "物品编号", name: "Code", width: 100, align: "left", sortable: false },
                     { label: "所属部位", name: "PSMemo", width: 90, align: "left", sortable: false },
                     { label: "规格型号", name: "ModelNo", width: 80, align: "left", sortable: false },
                     { label: "数量", name: "Num", width: 80, align: "left", sortable: false },
                     { label: "赔偿单价", name: "UnitPrice", width: 80, align: "left", sortable: false },
                     { label: "备注", name: "Memo", width: 80, align: "left", sortable: false },
                    ],
                    gridComplete: function () {
                        $("#" + subgrid_table_id).closest("div.ui-jqgrid-view").children("div.ui-jqgrid-titlebar").css("text-align", "left");
                    }
                });
            },

        });
      
    }




    //添加
    function btn_add() {
        var organizeid = _organizeid;
        if (organizeid == "") {
            dialogMsg("先选择组织机构。", 0);
            return false;
        }
        var Category = $("#Category").attr("data-value");
        if (Category === "" || Category == null) {
            dialogMsg('请选择业务类型', 0);
            return false;
        }
        var obj = $("#gridTable").jqGrid("getRowData");
        dialogOpen({
            id: "AddForm",
            title: '添加',
            url: '../ResourceManage/RS_PStructPublish/AddForm?organizeid=' + organizeid + "&Category=" + Category,
            width: "1100px",
            height: "600px",
            callBack: function (iframeId) {
                top.frames[iframeId].AcceptClick();
            }
        });

    };

    //移除
    function btn_delete_field() {
        var keyValue = $("#gridTable").getGridParam('selrow');
        if (keyValue) {
            dialogConfirm('注：您确定要移除吗？该操作将无法恢复？', function (r) {
                if (r) {
                    $("#gridTable").delRowData(keyValue);
                    dialogMsg("移除成功。", 1);
                }
            });

        } else {
            dialogMsg('请选择需要移除的数据！', 0);
        }
    }
   

    //保存
    function btn_Save() {
        if (!$('#PSBillfrom').Validform()) {
            return false;
        }
        var postData = $("#PSBillfrom").GetWebControls(keyValue);
        var obj = $("#gridTable").jqGrid("getRowData");      
        if (obj.length < 1) {
            dialogMsg('最少选择一条数据', 0);
            return false;
        }     
        for (var i = 0; i < obj.length; i++) {          
            if (obj[i].IsPublishToPC.indexOf("fa-toggle-on")>-1) {
                obj[i].IsPublishToPC = 1;
            }
            else {
                obj[i].IsPublishToPC = 0;
            }
            if (obj[i].IsPublishToOther.indexOf("fa-toggle-on") > -1) {
                obj[i].IsPublishToOther = 1;
            }
            else {
                obj[i].IsPublishToOther = 0;
            }
            if (obj[i].IsPublishToPC == 0 && obj[i].IsPublishToOther == 0) obj[i].IsPublishToPC = 1;           
        }     
        $.SaveForm({
            url: "../../ResourceManage/RS_PStructPublish/SavePStructPublishBillEntity",
            param: { keyValue: keyValue, type: 1, entity: JSON.stringify(postData), list: JSON.stringify(obj) },
            loading: "正在保存数据...",
            success: function () {
                $.currentIframe().$("#gridTable").resetSelection();
                $.currentIframe().$("#gridTable").trigger("reloadGrid");
            }
        })

    }
    //提交
    function btn_Submit() {
        if (!$('#PSBillfrom').Validform()) {
            return false;
        }
        var obj = $("#gridTable").jqGrid("getRowData");
        if (obj.length < 1) {
            dialogMsg('最少选择一条数据', 0);
            return false;
        }
        SecSpan = $("#SecSpan").val();
        var postData = $("#PSBillfrom").GetWebControls(keyValue);

        if (postData.Category == 1) {
            if (SecSpan === "") {
                dialogMsg('请输入可选房时间', 0);
                return false;
            }
            for (var i = 0; i < obj.length; i++) {
                if (obj[i].RoomType === "" || obj[i].RoomType == null) {
                    dialogMsg('请完善房屋户型信息后再提交', 0);
                    return false;
                }
                if (obj[i].Area === "" || obj[i].Area == null) {
                    dialogMsg('请完善房屋面积信息后再提交', 0);
                    return false;
                }
                if (obj[i].EMoveInDate === "" || obj[i].EMoveInDate == null) {
                    dialogMsg('请完善房屋齐起租日期后再提交', 0);
                    return false;
                }
                if (obj[i].MonthlyRent === "" || obj[i].MonthlyRent == null) {
                    dialogMsg('请完善房屋租金信息后再提交', 0);
                    return false;
                }
                if (obj[i].IsPublishToPC.indexOf("fa-toggle-on") > -1) {
                    obj[i].IsPublishToPC = 1;
                }
                else {
                    obj[i].IsPublishToPC = 0;
                }
                if (obj[i].IsPublishToOther.indexOf("fa-toggle-on") > -1) {
                    obj[i].IsPublishToOther = 1;
                }
                else {
                    obj[i].IsPublishToOther = 0;
                }
                if (obj[i].IsPublishToPC == 0 && obj[i].IsPublishToOther == 0) obj[i].IsPublishToPC = 1;
            }
        }
        if (postData.Category == 2) {
            for (var i = 0; i < obj.length; i++) {
                if (obj[i].IsPublishToPC.indexOf("fa-toggle-on") > -1) {
                    obj[i].IsPublishToPC = 1;
                }
                else {
                    obj[i].IsPublishToPC = 0;
                }
                if (obj[i].IsPublishToOther.indexOf("fa-toggle-on") > -1) {
                    obj[i].IsPublishToOther = 1;
                }
                else {
                    obj[i].IsPublishToOther = 0;
                }
                if (obj[i].IsPublishToPC == 0 && obj[i].IsPublishToOther == 0) obj[i].IsPublishToPC = 1;
            }
        }
       
        dialogConfirm('注：您确定要提交吗？该操作将无法恢复？', function (r) {
            if (r) {
                $.SaveForm({
                    url: "../../ResourceManage/RS_PStructPublish/SubmitForm",
                    param: { keyValue: keyValue, type: 2,secSpan:SecSpan, entity: JSON.stringify(postData), list: JSON.stringify(obj) },
                    loading: "正在保存数据...",
                    success: function (data) {
                        $.currentIframe().$("#gridTable").resetSelection();
                        $.currentIframe().$("#gridTable").trigger("reloadGrid");
                        dialogClose();
                    }
                })
            }
        })
    }

    //关闭
    function btn_Close() {
        dialogClose();
    }


</script>
<style>

</style>
<div style="width:98%;">
    <div style="height:7px;"></div>
    <input id="AiditType" type="hidden" />
    <table class="form" id="PSBillfrom" style="margin:0px auto;">
        <tr>
            <td class="formTitle">所属机构<font face="宋体">*</font></td>
            <td class="formValue" colspan="2">
                <div id="OrganizeId" type="selectTree" class="ui-select BaseClass" isvalid="yes" checkexpession="NotNull"></div>
            </td>
            <td class="formTitle">单据日期</td>
            <td class="formValue" colspan="2">
                <input id="Date" type="text" class="form-control input-wdatepicker BaseClass " placeholder="请输入单据日期" value="@LeSoft.Util.Time.GetToday("yyyy-MM-dd HH:mm:ss")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd HH:mm:ss' })" />
            </td>
            <td class="formTitle">单据编号</td>
            <td class="formValue" colspan="2">
                <input id="Code" type="text" class="form-control BaseClass" />
            </td>   
        </tr>
        <tr>
            <td class="formTitle">业务类型<font face="宋体">*</font></td>
            <td class="formValue" colspan="2">
                <div id="Category" type="selectTree" class="ui-select BaseClass" isvalid="yes" checkexpession="NotNull"></div>
            </td>
            <td class="formTitle CS-T">选房开放时间</td>
            <td class="formValue CS-T" colspan="2">
                <input id="SecSpan" type="text" class="form-control input-wdatepicker BaseClass " placeholder="请输入选房开放时间" value="@LeSoft.Util.Time.GetToday("yyyy-MM-dd HH:mm")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd  HH:mm' })" />
            </td>          
        </tr>
        <tr>
            <th class="formTitle">
                说明
            </th>
            <td class="formValue" colspan="8">
                <textarea id="Description" class="form-control BaseClass" style="height: 80px;" isvalid="yes" checkexpession="IsScript"></textarea>
            </td>
        </tr>
        <tr>
            <td></td>
            <td colspan="8"><div class="AiditInfo" style="line-height:20px; height:25px;"></div></td>
        </tr>
    </table>

</div>
<div style="margin: 5px; margin-top: 0px; margin-bottom: 0px;">
    <table id="gridTable"></table>
    <style>
        .ui-jqgrid .ui-jqgrid-caption {
            text-align: right;
            padding-right: 5px;
            background-color: #F5F5F5;
        }
    </style>
</div>
<div class="toolbar" style="position: absolute; top: 190px; left: 10px;">
    <a id="lr-addField" class="btn btn-success btn-xs BaseClass" onclick="btn_add()"><i class="fa fa-plus BaseClass"></i>&nbsp;添加</a>
    <a id="lr-deleteField" class="btn btn-danger btn-xs BaseClass" onclick="btn_delete_field()"><i class="fa fa-trash-o BaseClass"></i>&nbsp;移除</a>
</div>
<div class="form-button" id="wizard-actions">
    <a id="btn_last" onclick="btn_Save();" class="btn btn-success ">保存草稿</a>
    <a id="btn_next" onclick="btn_Submit();" class="btn btn-info ">提交</a>
    <a id="btn_finish" onclick="btn_Close();" class="btn btn-danger ">关闭</a>
</div>



